package cuShapes;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import javax.xml.bind.annotation.*;
import palette.*;
//import xml.*;
//import javax.xml.bind.annotation.adapters.*;

/*******************************************************
 * @author PQPeople
 * 
 * A Box has a starting 
 * A box has a rect on all 4 corners, and 1 in the center. 
 *   when we draw a new box, we are really just resizing from
 *   one of the corners.  
 *
 */

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="xmlbox")
public class Box extends Connectable {

	@XmlTransient
	private static final long serialVersionUID = 1L;
	@XmlTransient
	private static int nameCount = 0;
	
	/**************************************************************
	 * Implicit value constructor that sets all PQShape variables
	 * 
	 * make a box with the default size 100,100 centered by the mouse 
	 *   click
	 **************************************************************/
	public Box(Point centerPoint, Color c){
		this(new Point(centerPoint.x, centerPoint.y),
				new Point(centerPoint.x+5, centerPoint.y+5), c);
	}

	/*********************************************************************
	 * constructor
	 *   Create empty box with default dimensions. 
	 *********************************************************************/
	public Box() {
		this(new Point(0, 0), new Point (0, 0), CurrentColor.getInstance());
	}

	/**************
	 * Creates new box with the given 2 pt dimension
	 * 
	 * @param p1 Point that represents the topLeft of the box
	 * @param p2 Point that represents the bottomRight of the box
	 * @param c  Color used 
	 */
	public Box(Point p1, Point p2, Color c) {
		
		super();
		color = c;
		name = "Box #" + Integer.toString(nameCount++);
		/*
		 * Lines 0 and 1 start at the same point (the top left corner, originally).
		 * Lines 2 and 3 start at the bottom right corner.
		 * end1 is the top right
		 * end2 is the bottom left
		 */
		Point end1 = new Point((int)p2.x, (int)p1.y);
		Point end2 = new Point((int)p1.x, (int)p2.y);
		lines.add(new StraightLine(p1, end1, c));
		lines.add(new StraightLine(p1, end2, c));
		lines.add(new StraightLine(p2, end1, c));
		lines.add(new StraightLine(p2, end2, c));
		width = p2.x-p1.x;
		height = p2.y-p1.y;
		//Set the center and resize
		center = new Rectangle(p1.x+(width/2)-5,p1.y+(height/2)-5,10,10);
		resize = new Rectangle(p2.x-5,p2.y-5,10,10);
		rect = new Rectangle(p1.x,p1.y,width, height);
	}
	
	public void setStart(Point p) {
		super.setStart(p);
		lines.get(0).setStart(p);
		lines.get(1).setStart(p);
	}

	public void setEnd(Point p) {
		super.setEnd(p);
		lines.get(2).setStart(p);
		lines.get(3).setStart(p);
		Point p2 = lines.get(0).getStart();
		lines.get(1).setEnd(new Point(p2.x, p.y));
		lines.get(0).setEnd(new Point(p.x, p2.y));
		lines.get(2).setEnd(new Point(p2.x, p.y));
		lines.get(3).setEnd(new Point(p.x, p2.y));
		resize.setLocation(p.x-5, p.y-5);
	}
	
	public Point getEnd() {
		return lines.get(3).getStart();
	}
	
	/*************************************
	 *   update Wdith and Height
	 */
	private void updateDimensions() {
		width = Math.abs(lines.get(3).getStart().x - lines.get(0).getStart().x);
		height = Math.abs(lines.get(3).getStart().y - lines.get(0).getStart().y);
	}
	
	public void moveWorkingRect(Point p){
		   //moving.
		if(workingRect == center){
			//base everything off the center bc thats where the mouse click was
			center.setLocation(p.x-5,p.y-5);
			setStart(new Point(p.x-width/2, p.y-height/2));
			setEnd(new Point(p.x+width/2, p.y+height/2));
			rect.setLocation(p.x-width/2, p.y-height/2);
			
		}
		else if(workingRect == rect){
			
		}
		//resizing
		else{
			int xx = (p.x > lines.get(0).getStart().x) ? p.x : lines.get(0).getStart().x;
			int yy = (p.y > lines.get(0).getStart().y) ? p.y : lines.get(0).getStart().y;
			setEnd(p);
			updateDimensions();
			center.setLocation(xx-width/2-5, yy-height/2-5);
			//rect.setBounds(lines.get(0).getStart().x,lines.get(0).getStart().y, width, height);
			int ix = (p.x < lines.get(0).getStart().x) ? p.x : lines.get(0).getStart().x;
			int iy = (p.y < lines.get(0).getStart().y) ? p.y : lines.get(0).getStart().y;
			
			rect.setBounds(ix+2,iy+2, width-4, height-4);
		}
	}

	public String getShapeClass() {
		return "Box";
	}
	
	public static void resetNames() {
		nameCount = 0;
	}
	
	public int getWidth() {
		return Math.abs(width);
	}
	
	public int getHeight() {
		return Math.abs(height);
	}

	/******************************************
	 * This smart shape will determine which 
	 *   (if any) of its rects have been hit.
	 * If a rect is hit, set it to the workingRect, 
	 *    and return true.
	 *  NOTE: getWorkingRect() is most likely called
	 *    right after this. 
	 */
	public boolean hitRect(Point p) {
		boolean ret = false;
		if(center.contains(p)){
			workingRect = center;
			ret = true;
		}
		else if(resize.contains(p)){
			workingRect = resize;
			ret = true;
		}
		return ret;
	}
	
}
